package com.treetao.union.web.controller.mall;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treetao.union.db.model.MallSkill;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.MallSkillService;
import com.treetao.union.web.constant.Constants;

/**
 * <p>
 * 秒杀活动 前端控制器
 * </p>
 *
 * @author Dev.M
 * @since 2020-05-21
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/mall-skill")
public class MallSkillController {

	@Autowired
	private MallSkillService mallSkillService;

	@GetMapping("list")
	public String list() {
		return "/admin/mall-skill/list";
	}

	@PostMapping("list")
	@ResponseBody
	public LayerData<MallSkill> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, ServletRequest request) {
		Map map = WebUtils.getParametersStartingWith(request, "s_");
		LayerData<MallSkill> layerData = new LayerData<>();
		QueryWrapper<MallSkill> wrapper = new QueryWrapper<MallSkill>();
		if (!map.isEmpty()) {
			String delFlag = (String) map.get("delFlag");
			if (StringUtils.isNotBlank(delFlag)) {
				wrapper.eq("del_flag", Boolean.valueOf(delFlag));
			} else {
				map.remove("del_flag");
			}
		}
		wrapper.orderByAsc("del_flag");
		wrapper.orderByDesc("startTime");
		IPage<MallSkill> pageData = mallSkillService.page(new Page<>(page, limit), wrapper);
		layerData.setData(pageData.getRecords());
		layerData.setCount(pageData.getTotal());
		return layerData;
	}

	@GetMapping("add")
	public String add() {
		return "/admin/mall-skill/add";
	}

	@PostMapping("add")
	@ResponseBody
	public RestResponse add(MallSkill mallSkill) {
		if (mallSkillService.isExistBetweenTime(mallSkill.getStartTime(), mallSkill.getEndTime())!=null) {
			return RestResponse.failure("该时间段，已存在秒杀活动");
		}
		mallSkillService.save(mallSkill);
		return RestResponse.success();
	}

	@GetMapping("detail")
	public String detail(Long id, Model model) {
		MallSkill mallSkill = mallSkillService.getById(id);
		model.addAttribute("mallSkill", mallSkill);
		return "/admin/mall-skill/detail";
	}

	@GetMapping("edit")
	public String edit(Long id, Model model) {
		MallSkill mallSkill = mallSkillService.getById(id);
		model.addAttribute("mallSkill", mallSkill);
		return "/admin/mall-skill/edit";
	}

	@PostMapping("edit")
	@ResponseBody
	public RestResponse edit(MallSkill mallSkill) {
		if (null == mallSkill.getId() || 0 == mallSkill.getId()) {
			return RestResponse.failure("ID不能为空");
		}
		Long skillId = mallSkillService.isExistBetweenTime(mallSkill.getStartTime(), mallSkill.getEndTime());
		if(skillId!=null && skillId.longValue() != mallSkill.getId().longValue()) {
			return RestResponse.failure("该时间段，已存在秒杀活动");
		}
		if(mallSkillService.started(mallSkill.getId())) {
			return RestResponse.failure("秒杀进行中，暂时不能修改");
		}
		mallSkillService.updateById(mallSkill);
		return RestResponse.success();
	}

	@PostMapping("batchDel")
	@ResponseBody
	public RestResponse batchDel(@RequestParam(value = "ids[]", required = false) List<Long> ids) {
		if (null == ids || ids.size() == 0) {
			return RestResponse.failure("请选择一项");
		}
		return RestResponse.success().setDataOnlyResult(mallSkillService.removeByIds(ids));
	}

	@PostMapping("delete")
	@ResponseBody
	public RestResponse delete(@RequestParam(value = "id", required = false) Long id) {
		if (null == id || 0 == id) {
			return RestResponse.failure("ID不能为空");
		}
		return RestResponse.success().setDataOnlyResult(mallSkillService.removeById(id));
	}

	@PostMapping("change-status")
	@ResponseBody
	public RestResponse change_status(@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "status", required = false) Boolean status) {
		MallSkill mallSkill = mallSkillService.getById(id);
		
		if(status && mallSkill!=null) {
			if (mallSkillService.isExistBetweenTime(mallSkill.getStartTime(), mallSkill.getEndTime())!=null) {
				return RestResponse.failure("该时间段，已存在秒杀活动");
			}
		}
		
		mallSkill.setDelFlag(status);
		mallSkillService.updateById(mallSkill);
		return RestResponse.success();
	}

}